#pragma once

#include <QTableView>
#include <QStandardItemModel>
#include <QTimer>
#include "data.hpp"
#include "dialog.hpp"
#include "table.hpp"

namespace cloud
{

class View : public QTableView
{
    Q_OBJECT
public:
    View()
        : _table(new Table(this))
        , _model(new QStandardItemModel(this))
        , _data_mgr(new Data)
    {
        INFO("data model and data manager initialize success");
        INFO("table view initialize success");

        _table->setTable();
        QTableView::connect(_table, &QTableView::doubleClicked, this, &View::OpenDialog);

        int time = CmdHelper::instance()->value("t").toInt();
        if (time < 5) time = 5;

        QTimer* timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &View::fillData);
        timer->start(time * 1000); // interval milliseconds

        INFO("timer initialize success, request data every " + QString::number(time) + " seconds");
    }

    void fillData()
    {
        QVector<Info>* datas = nullptr;
        _data_mgr->getData(datas); // Get data

        if (!datas) return; // Every time we refresh data , the program will freeze.
                            // So we fresh it, just when we need to do it.

        _model->clear(); // Clear data model

        // Set table headers
        QStringList labels = QObject::tr("file name,file size,is hot,modify time").simplified().split(",");
        _model->setHorizontalHeaderLabels(labels);

        // Fill content
        for (const Info& data : *datas)
        {
            QList<QStandardItem*> row;
            row << new QStandardItem(data._fname)
                << new QStandardItem(QString::number(data._fsize))
                << new QStandardItem(data._ishot ? "true" : "false")
                << new QStandardItem(data._mtime);
            for (auto& item : row)
                item->setTextAlignment(Qt::AlignCenter);
            _model->appendRow(row);
        }

        // Set data model in table
        _table->setModel(_model);
        _table->setColumsWidth(40, 15, 15, 30);
        INFO("request and load data model success");
    }

    void show()
    {
        _table->show();
        QWidget::show(); // we have to call this show funcition, because view has no parent
    }

    ~View()
    {
        delete _data_mgr;
    }

public slots:
    void OpenDialog(const QModelIndex& index)
    {
        QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_table->model());
        if (!model)
            THROW("get tableview's data model failed");
        QVariant name = model->data(model->index(index.row(), 0));

        showResource(name); // Use a text box show the file content,
                                // when user clicked the filelist row
    }

private:
    void showResource(const QVariant& name)
    {
        QString uri = "/download/" + name.toString();
        ResDialog *dialog = new ResDialog(uri, this);

        dialog->setAttribute(Qt::WA_DeleteOnClose);
        dialog->setResDialog();
        dialog->show();
    }
private:
    Table* _table;
    QStandardItemModel* _model;
    Data* _data_mgr;
};

}
